package com.walter.controller;

import cn.hutool.core.lang.Dict;
import com.walter.Application;
import com.walter.annotation.SysLog;
import com.walter.common.BaseController;
import com.walter.common.PageVO;
import com.walter.common.Result;
import com.walter.consts.DictConst;
import com.walter.model.form.AddUserForm;
import com.walter.model.form.BindRolesForm;
import com.walter.model.form.UpdatePwdForm;
import com.walter.model.query.UserQuery;
import com.walter.model.vo.UserInfoVO;
import com.walter.model.vo.UserVO;
import com.walter.model.vo.UserVODetail;
import com.walter.service.UserService;
import com.walter.util.ValidationUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：ch
 * @date ：Created in 2021/5/25
 * @description：
 * @modified By：
 * @version: 1.0
 */
@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserController extends BaseController {
    @Resource
    private UserService userService;

    @GetMapping("info")
    @ApiOperation("后管 查询当前用户信息")
    public Result<UserVODetail> info(@RequestParam String token){
        log.info("token is:\n {}",token);
        return success(userService.info(token));
    }

    @GetMapping("logout")
    @ApiOperation("后管 登出")
    public Result logout(){
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String userName = (String)auth.getPrincipal();
        log.info("{} logout success",userName); // 待优化 todo
        return success();
    }

    @PostMapping("add")
    @ApiOperation("后管 添加用户")
    @SysLog("后管 添加用户")
    public Result<Integer> add(@RequestBody AddUserForm form){
        ValidationUtils.validateEntity(form);
        return success(userService.add(form));
    }

    @GetMapping("findAllGender")
    @ApiOperation("后管 查询所有性别")
    public Result<List<Dict>> findAllGender(){
        return success(Application.secondLevelDictByFirst.get(DictConst.GENDER));
    }

    @PostMapping("update")
    @ApiOperation("后管 修改用户")
    @SysLog("后管 修改用户")
    public Result<Boolean> update(@RequestBody UserInfoVO userInfoVO){
        ValidationUtils.validateEntity(userInfoVO);
        return success(userService.update(userInfoVO));
    }

    @PostMapping("find")
    @ApiOperation("后管 分页查询")
    public Result<PageVO<UserVO>> find(@RequestBody(required = true) UserQuery query){
        return success(userService.find(query));
    }

    @GetMapping("findOne/{userId}")
    @ApiOperation("后管 查询单个用户详情")
    public Result<UserInfoVO> findOne(@PathVariable Integer userId){
        return success(userService.findOne(userId));
    }

    @GetMapping("resetPwd")
    @ApiOperation("后管 重置密码")
    @SysLog("后管 重置密码")
    public Result<Boolean>  resetPwd(@RequestParam Integer userId){
        return success(userService.resetPwd(userId));
    }

    @PostMapping("updatePwd")
    @ApiOperation("后管 修改密码")
    @SysLog("后管 修改密码")
    public Result<Boolean> updatePwd(@RequestBody UpdatePwdForm form){
        return success(userService.updatePwd(form));
    }

    @GetMapping("toggleIsEnabled")
    @ApiOperation("后管 切换用户是否启用")
    @SysLog("后管 切换用户是否启用")
    public Result<Integer> toggleIsEnabled(@RequestParam("userId") Integer userId){
        return success(userService.toggleIsEnabled(userId));
    }

    @PostMapping("bindRoles")
    @ApiOperation("后管 用户绑定角色")
    @SysLog("后管 用户绑定角色")
    public Result<Integer> bindRoles(@RequestBody BindRolesForm form){
        ValidationUtils.validateEntity(form);
        return success(userService.bindRoles(form));
    }
}
